﻿/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is tyzoh.jp code.
 *
 * The Initial Developer of the Original Code is Kazutaka Kurihara.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** */
using System;
using System.Drawing;
using System.Windows.Forms;
using PluginInterface;
using Qurihara;
//using ViewSnapPlugin;

namespace KotodamaPlugin.PPT2007DragDropPlugin
{
	/// <summary>
	/// Class1 の概要の説明です。
	/// </summary>
	public class PPT2007DragDropPlugin :IPlugin
	{
		public PPT2007DragDropPlugin()
		{
		}

		private void DropHandler(object o, Qurihara.DragDrop.FileDropEventArgs args)
		{	
			//ViewSnapPlugin.ViewSnapPlugin plugin = (ViewSnapPlugin.ViewSnapPlugin)Host.Kotodama.EnvironmentTable["ViewSnap Plugin"];
			ViewSnapList vsl = (ViewSnapList)Host.Kotodama.EnvironmentTable["CurrentViewSnapList"];

			Array a = args.DroppedFilesArray;
			Point dp = args.DropPoint;
            if (args.DataFormat != DataFormats.FileDrop) return;

			//System.Windows.Forms.Form form = Host.Kotodama.Form;
			System.Windows.Forms.Control form = Host.Kotodama.Control;
			Qurihara.Slide slide = Host.Kotodama.Presentation.CurrentSlide;

			//slide.View.ScreenToView(ref dp);

			string sError = "";
			string sFile;

			sFile = a.GetValue(0).ToString();           // file name

			System.IO.FileInfo info = new System.IO.FileInfo(sFile);

            string ext = System.IO.Path.GetExtension(sFile);

            //if (sFile.Length > 4) ext = sFile.Substring(sFile.Length-4).ToLower();
            //else ext = "xxx";

			Point p = form.PointToClient(dp);
			Host.Kotodama.Presentation.CurrentSlide.View.ScreenToView(ref p);

			if (info.Exists)
			{
				if(ext == ".ppt" || ext == ".pptx")
				{
					KotodamaPlugin.PPT2007DragDropPlugin.PPTDropLayoutDialog dialog = new KotodamaPlugin.PPT2007DragDropPlugin.PPTDropLayoutDialog();
					dialog.ShowDialog();
					if (dialog.LayoutSet)
					{
						//if (true == PPTImport.PPTImportImages(sFile,p,Host.Kotodama.Presentation.CurrentSlide,dialog.Layout,plugin))
						if (true == PPTImport.PPTImportImages(sFile,p,Host.Kotodama.Presentation.CurrentSlide,dialog.Layout,vsl))
						{
							//ここでいったんスライドをセーブすればコピー未遂は無くなる。
							//Host.Kotodama.Presentation.goNewSlide();
							//Host.Kotodama.Presentation.goPrevSlide();
						
							//Host.Kotodama.Form.BringToFront();
							//Host.Kotodama.Form.Activate();
							Form f = (Form)Host.Kotodama.EnvironmentTable["Base.Form"];
							if (f != null) f.Activate();
						}
					}
				}
			}

			if ( sError.Length > 0 )
			{
				//MessageBox.Show(form, sError, "Open File Error");
				//System.Diagnostics.Debug.Fail("Open File Error");
			}
		}

		private Rectangle SizeFitToForm(Point p, Bitmap bm)
		{
			Rectangle r = new Rectangle(
				p.X,p.Y,
				bm.Width,
				bm.Height);
			while(!Host.Kotodama.Control.ClientRectangle.Contains(r))
			{
				r.Width /= 2;
				r.Height /= 2;				
			}
			return r;
		}

		#region IPlugin Members
		
		IPluginHost myPluginHost = null;
		string myPluginName = "ppt drop  Plugin";
		string myPluginAuthor = "K.Qurihara";
		string myPluginDescription = "test plugin";
		string myPluginVersion = "1.0.0";
		
        
		void PluginInterface.IPlugin.Dispose()
		{
			// TODO:  Add ctlMain.PluginInterface.IPlugin.Dispose implementation
			Host.Kotodama.FormFilesDropEventHandler -= new Qurihara.DragDrop.FilesDropEventHandler(this.DropHandler);
		}

		public string Description
		{
			get
			{
				// TODO:  Add ctlMain.Description getter implementation
				return myPluginDescription;
			}
		}

		public string Author
		{
			get
			{
				// TODO:  Add ctlMain.Author getter implementation
				return myPluginAuthor;
			}
		}

		public IPluginHost Host
		{
			get
			{
				// TODO:  Add ctlMain.Host getter implementation
				return myPluginHost;
			}
			set
			{
				myPluginHost = value;
			}
		}

		public void Initialize()
		{
			// TODO:  Add ctlMain.Initialize implementation
			Host.Kotodama.FormFilesDropEventHandler += new Qurihara.DragDrop.FilesDropEventHandler(this.DropHandler);
		}

		
		public string Name
		{
			get
			{
				// TODO:  Add ctlMain.Name getter implementation
				return myPluginName;
			}
		}
		

		public string Version
		{
			get
			{
				// TODO:  Add ctlMain.Version getter implementation
				return myPluginVersion;
			}
		}

		#endregion


	}
}
